import { create } from 'zustand'
import { persist, createJSONStorage } from 'zustand/middleware'
import { immer } from "zustand/middleware/immer"

import { switchDarkTheme } from '@/hooks/switchDarkTheme'

export const useCommonStore = create(immer(
    persist(
        () => ({
            isDark: false
        })
        , {
            name: 'commonStore',
            storage: createJSONStorage(() => localStorage),
        }
    )
)
)

export const switchDark = (isDark: boolean = true) => {
    useCommonStore.setState((state) => {
        state.isDark = switchDarkTheme(isDark)
        LDAPI.onDarkModeChange([state.isDark])
    })
}

useCommonStore.subscribe((state) => {
    console.log(state.isDark)
})
